package com.sam.widget.button;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import com.sam.widget.R;

/**
 * Created by Administrator on 2016/6/13.
 */
public class StateImageButton extends ImageView {
    private int maskColor=0x40000000;
    private boolean initialized=false;
    public StateImageButton(Context context) {
        this(context,null);
    }

    public StateImageButton(Context context, AttributeSet attrs) {
        this(context, attrs,android.R.attr.imageButtonStyle);
    }

    public StateImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);


        TypedArray ta=context.obtainStyledAttributes(attrs, R.styleable.StateImageButton);
        int maskColor=ta.getColor(R.styleable.StateImageButton_maskColor,0x40000000);
        ta.recycle();
        addStateEffet();
        initialized=true;
//
//        if(getDrawable()!=null) {
//            setImageDrawable(createStateDrawable(getDrawable(),maskColor));}
//        else if(getBackground()!=null){
//            setBackgroundDrawable(createPressedDrawable(getBackground(),maskColor));}

    }

    private void addStateEffet() {
        Drawable srcDrawable=getDrawable();
        Drawable bgDrawable=getBackground();
        if(srcDrawable!=null&& srcDrawable instanceof BitmapDrawable)
        {
            setImageDrawable(createStateDrawable((BitmapDrawable) srcDrawable,maskColor));
        } else if(bgDrawable != null && bgDrawable instanceof BitmapDrawable){
            setBackgroundDrawable(createStateDrawable((BitmapDrawable) bgDrawable,maskColor));
        }
    }
    public  void setImageDrawable(Drawable drawable){
        super.setImageDrawable(drawable);
        if(initialized) addStateEffet();
    }
    public  void setBackgroundDrawable(Drawable background){
        super.setBackgroundDrawable(background);
        if(initialized) addStateEffet();
    }
    public  void setImageResourse(Drawable resId){
        super.setImageDrawable(resId);
        if(initialized) addStateEffet();
    }
    public  void setBackgroundResourse(Drawable resid){
        super.setBackgroundDrawable(resid);
        if(initialized) addStateEffet();

    }

    public void setBackground(Drawable background){
        super.setBackground(background);
        if(initialized) addStateEffet();
    }


    private StateListDrawable createStateDrawable(Drawable drawable,int maskColor){
        StateListDrawable stateListDrawable=new StateListDrawable();
        stateListDrawable.addState(View.PRESSED_ENABLED_STATE_SET,createPressedDrawable(drawable,maskColor));//按压状态
        stateListDrawable.addState(View.EMPTY_STATE_SET,drawable);//默认状态
        return stateListDrawable;
    }
    private Drawable createPressedDrawable(Drawable drawable,int maskColor){
        Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap().copy(Bitmap.Config.ARGB_8888,true);//Bitmap图像处理：复制一个可变的
        int width=bitmap.getWidth();
        int height=bitmap.getHeight();
        int len=width*height;
        int[] pixels=new int[len];
        bitmap.getPixels(pixels,0,width,0,0,width,height);//获得bitmap的像素颜色值，赋值时给pixels
        for(int i=0;i<len;i++){
            if(pixels[i]!=0x00000000 ) pixels[i]=maskColor;
        }
        new Canvas(bitmap).drawBitmap(Bitmap.createBitmap(pixels,width,height, Bitmap.Config.ARGB_8888),0,0,null);//画图
        return new BitmapDrawable(bitmap);

    }

}
